package com.sherlocky.oss.qiniu.handler;

import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sherlocky.oss.qiniu.bean.MyRet;
import com.sherlocky.oss.qiniu.constants.QiniuOSSConstants;
import com.sherlocky.oss.qiniu.util.QiniuOSSUtil;
import com.sherlocky.util.PropertiesUtil;

public class QiniuOSSHandler {
	
	private static final Log log = LogFactory.getLog(QiniuOSSHandler.class);
	// 存储的空间
	// 存储的空间
	private static final String BUCKET = PropertiesUtil.getValue(QiniuOSSConstants.PROPERTIES, QiniuOSSConstants.BUCKET, "sherlocky");
	private static final String FOPS = PropertiesUtil.getValue(QiniuOSSConstants.PROPERTIES, QiniuOSSConstants.FOPS, "avthumb/m3u8/segtime/10/preset/video_640k");
	private static final String NOTIFY_URL = PropertiesUtil.getValue(QiniuOSSConstants.PROPERTIES, QiniuOSSConstants.NOTIFY_URL);
	
	/*
	public static boolean deleteFile(String key) {
		return QiniuOSSUtil.delete(OSS_BUCKET, key);
	}
	
	public static boolean existsFile(String key) {
		Entry stat = QiniuOSSUtil.getFileInfo(OSS_BUCKET, key);
		return stat.ok();
	}
	
	public static void uploadFile2OSS(String key, String filepath) throws AuthException, JSONException, FileNotFoundException {
		QiniuOSSUtil.upload(OSS_BUCKET, key, filepath);
	}
	
	public static void uploadFile2OSS(String key, InputStream in) throws AuthException, JSONException {
		QiniuOSSUtil.upload(OSS_BUCKET, key, in);
	}
	 */
	
	public static void uploadFile2OSSAndTrans(String key, File file) {
		// 重命名转码文件
		String fops = FOPS + "|saveas/" + QiniuOSSUtil.urlsafeEncode(BUCKET + ":" + key + ".m3u8");
		log.info("执行上传OSS操作：文件名[" + key + "]，上传位置[" + BUCKET + "]，转码命令[" + fops + "]，转码存放位置[" + BUCKET + "]，通知接口[" + NOTIFY_URL + "]");
		MyRet ret = QiniuOSSUtil.upload(BUCKET, key, file, fops, NOTIFY_URL);
		log.info(ret);
		if (ret != null) {
			log.info("成功上传到OSS，hash[" + ret.getHash() + "]，response[" + ret.response + "]");
		}
	}
	
	public static String getOSSBucket() {
		return BUCKET;
	}
	
	public static void main(String[] args) {
		File f = new File("D:/Sherlocky/Desktop/linux_13_basic_commands_7.avi");
		uploadFile2OSSAndTrans(f.getName(), f);
	}
}
